package com.example.medical.util;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

/**
 * 
 * <pre>
 * 描述：趣医医院+SSL双向认证测试
 * 作者：lijunqiang 
 * 时间：2016年7月14日下午10:11:41
 * 类名: SSLClientDemo
 * </pre>
 */
public class SSLClientDemo {
    public static void main(String[] args) throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // ResourceUtils.getFile("classpath:resource/static/thirdPartyTrust/quyi/person-client.p12");

        keyStore.load(
                new FileInputStream(new File(
                        "D:/Users/lilei193/Downloads/siapp-city-web-develop-cd7a3859feb97cf0304e8d2056507053bd415b59/src/main/resources/static/thirdPartyTrust/quyi/person-client.p12")),
                "PARST310104.10074.hkbd@20170801".toCharArray());

        // ResourceUtils.getFile("classpath:resource/static/thirdPartyTrust/quyi/quyi-Cloud-truststore.jks");

        SSLContext sslcontext = SSLContexts.custom()
                // 加载服务端提供的truststore(如果服务器提供truststore的话就不用忽略对服务器端证书的校验了)
                .loadTrustMaterial(
                        new File(
                                "D:/Users/lilei193/Downloads/siapp-city-web-develop-cd7a3859feb97cf0304e8d2056507053bd415b59/src/main/resources/static/thirdPartyTrust/quyi/quyi-Cloud-truststore.jks"),
                        "quyicastore@20160706".toCharArray(), new TrustSelfSignedStrategy())
                .loadKeyMaterial(keyStore, "PARST310104.10074.hkbd@20170801".toCharArray()).build();
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext,
                new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();

        try {

            HttpGet httpget = new HttpGet("https://open.quyiyuan.com:8443/v1.0/");
            System.out.println("Executing request " + httpget.getRequestLine());

            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println(response.getStatusLine());
                System.out.println(IOUtils.toString(entity.getContent()));
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}